package com.gift.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gift.auto.JSONAuthentication;
import com.gift.entity.GiComment;
import com.gift.entity.GiProduct;
import com.gift.entity.UserMeans;
import com.gift.mapper.GiCommentMapper;
import com.gift.mapper.GiLpcMapper;
import com.gift.mapper.GiUserMapper;
import com.gift.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gift
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/gi-comment")
public class GiCommentController extends JSONAuthentication {
    @Autowired
    GiCommentMapper commentMapper;
    @Autowired
    GiUserMapper giUserMapper;
    @Autowired
    GiLpcMapper giLpcMapper;
    @ApiOperation(value = "用户对礼品册做出评论")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody GiComment giComment) throws Exception {
            if(commentMapper.insert(giComment)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
    }
    @ApiOperation(value = "删除礼品册评论")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = commentMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据礼品册查询评论")
    @PostMapping("/findAllPro")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("lpcId") int lpcid,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("lpc_id",lpcid);
        Page<GiComment> shUserPage=new Page<>(current,size);
        Page<GiComment> shUserPages=commentMapper.selectPage(shUserPage,wrapper);
        List<GiComment> records = shUserPages.getRecords();
        int total=commentMapper.selectCount(wrapper);
        for (GiComment a:records){
            if(a.getLpcId()!=null){
                a.setGiLpc(giLpcMapper.selectById(a.getLpcId()));
            }
            if(a.getUserId()!=null){
                a.setGiUser(giUserMapper.selectById(a.getUserId()));
            }
        }
        if(records.size()>0){
            Result ok = Result.ok().data("comment",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

